﻿//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EffiProzDB;
using EffiProzDB.Lib;
using System.Threading;
using EffiProzDB.Persist;


namespace EffiProzDBTest.SelfTest
{
    /// <summary>
    /// Summary description for TestPersistence
    /// </summary>
    [TestClass]
    public class TestPersistence
    {
       // SessionInterface sessionProxy;
        String user = "sa";
        String password = "";
        String connType = DatabaseURL.S_FILE;
       // String host = "localhost";
       // int port = 0;
       // String path = "Test";
       // String database = "Test";
       // string sql = "";
        HsqlProperties props = new HsqlProperties();
        string testScriptPath = "../../../EffiProzDBTest/SelfTest/Scripts/";
        string dataPath = "../../../EffiProzDBTest/Data/";

        public TestPersistence()
        {
           // sessionProxy = DatabaseManager.newSession(connType, database,
           //user, password, props); 

          
        }

        public void CleanUp(string prefix)
        {
            string[] files = Directory.GetFiles(dataPath,prefix+"*");
            foreach (var file in files)       
                File.Delete(file);
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestSelfSchemaPersistA()
        {
            CleanUp("PersistA");
            SessionInterface sessionProxy1 = DatabaseManager.newSession(connType, dataPath + "PersistA", user, password, props);
            string scriptPath = testScriptPath + "TestSelfSchemaPersistA1.txt";
            TestUtil.testScript(sessionProxy1, scriptPath);
            sessionProxy1.close();

            Thread.Sleep(1000);

            SessionInterface sessionProxy2 = DatabaseManager.newSession(connType, dataPath + "PersistA", user, password, props);
            scriptPath = testScriptPath + "TestSelfSchemaPersistA2.txt";
            TestUtil.testScript(sessionProxy2, scriptPath);
        }

        [TestMethod]
        public void TestSelfSchemaPersistB()
        {
            CleanUp("PersistB");
            SessionInterface sessionProxy1 = DatabaseManager.newSession(connType, dataPath + "PersistB", user, password, props);
            string scriptPath = testScriptPath + "TestSelfSchemaPersistB1.txt";
            TestUtil.testScript(sessionProxy1, scriptPath);
            sessionProxy1.close();

            Thread.Sleep(1000);

            SessionInterface sessionProxy2 = DatabaseManager.newSession(connType, dataPath + "PersistB", user, password, props);
            scriptPath = testScriptPath + "TestSelfSchemaPersistB2.txt";
            TestUtil.testScript(sessionProxy2, scriptPath);
        }

        [TestMethod]
        public void TestSelfSchemaPersistC()
        {
            CleanUp("PersistC");
            SessionInterface sessionProxy1 = DatabaseManager.newSession(connType, dataPath + "PersistC", user, password, props);
            string scriptPath = testScriptPath + "TestSelfSchemaPersistC1.txt";
            TestUtil.testScript(sessionProxy1, scriptPath);
            sessionProxy1.close();

            Thread.Sleep(1000);

            SessionInterface sessionProxy2 = DatabaseManager.newSession(connType, dataPath + "PersistC", user, password, props);
            scriptPath = testScriptPath + "TestSelfSchemaPersistC2.txt";
            TestUtil.testScript(sessionProxy2, scriptPath);
        }

        [TestMethod]
        public void TestSelfSchemaPersistD()
        {
            CleanUp("PersistD");
            SessionInterface sessionProxy1 = DatabaseManager.newSession(connType, dataPath + "PersistD", user, password, props);
            string scriptPath = testScriptPath + "TestSelfSchemaPersistD1.txt";
            TestUtil.testScript(sessionProxy1, scriptPath);
            sessionProxy1.close();

            Thread.Sleep(1000);

            SessionInterface sessionProxy2 = DatabaseManager.newSession(connType, dataPath + "PersistD", user, password, props);
            scriptPath = testScriptPath + "TestSelfSchemaPersistD2.txt";
            TestUtil.testScript(sessionProxy2, scriptPath);
        }

        [TestMethod]
        public void TestSelfSchemaPersistEBinaryScript()
        {
            CleanUp("PersistEB");
            HsqlProperties props2 = new HsqlProperties();
            props2.setProperty(HsqlDatabaseProperties.hsqldb_script_format, 1);
            SessionInterface sessionProxy1 = DatabaseManager.newSession(connType, dataPath + "PersistEB", user, password, props2);
            //TestHelper.ExecuteUpdateSQL_Success(sessionProxy1, "SET SCRIPTFORMAT BINARY;");

            string scriptPath = testScriptPath + "TestSelfSchemaPersistE1.txt";
            TestUtil.testScript(sessionProxy1, scriptPath);
            sessionProxy1.close();

            Thread.Sleep(1000);

            SessionInterface sessionProxy2 = DatabaseManager.newSession(connType, dataPath + "PersistEB", user, password, props2);
            scriptPath = testScriptPath + "TestSelfSchemaPersistE2.txt";
            TestUtil.testScript(sessionProxy2, scriptPath);
            sessionProxy2.close();
        }

        [TestMethod]
        public void TestSelfSchemaPersistZipScript()
        {
            CleanUp("PersistEZ");
            HsqlProperties props2 = new HsqlProperties();
            props2.setProperty(HsqlDatabaseProperties.hsqldb_script_format, 3);
            SessionInterface sessionProxy1 = DatabaseManager.newSession(connType, dataPath + "PersistEZ", user, password, props2);
            //TestHelper.ExecuteUpdateSQL_Success(sessionProxy1, "SET SCRIPTFORMAT BINARY;");

            string scriptPath = testScriptPath + "TestSelfSchemaPersistE1.txt";
            TestUtil.testScript(sessionProxy1, scriptPath);
            sessionProxy1.close();

            Thread.Sleep(1000);

            SessionInterface sessionProxy2 = DatabaseManager.newSession(connType, dataPath + "PersistEZ", user, password, props2);
            scriptPath = testScriptPath + "TestSelfSchemaPersistE2.txt";
            TestUtil.testScript(sessionProxy2, scriptPath);
            sessionProxy2.close();
        }

        [TestMethod]
        public void TestSelfSchemaPersistCTblTest()
        {
            CleanUp("CTblTest");
            SessionInterface sessionProxy1 = DatabaseManager.newSession(connType, dataPath + "CTblTest", user, password, props);
            string scriptPath = testScriptPath + "TestSelfSchemaPerCachTbl1.txt";
            TestUtil.testScript(sessionProxy1, scriptPath);
            sessionProxy1.close();

            Thread.Sleep(1000);

            SessionInterface sessionProxy2 = DatabaseManager.newSession(connType, dataPath + "CTblTest", user, password, props);
            scriptPath = testScriptPath + "TestSelfSchemaPerCachTbl2.txt";
            TestUtil.testScript(sessionProxy2, scriptPath);
        }

        [TestMethod]
        public void TestSelfTempTable1()
        {
            CleanUp("TempTable");
            SessionInterface sessionProxy1 = DatabaseManager.newSession(connType, dataPath + "TempTable", user, password, props);
            string scriptPath = testScriptPath + "TestSelfTempTable1.txt";
            TestUtil.testScript(sessionProxy1, scriptPath);
            sessionProxy1.close();

            Thread.Sleep(1000);

            SessionInterface sessionProxy2 = DatabaseManager.newSession(connType, dataPath + "TempTable", user, password, props);
            scriptPath = testScriptPath + "TestSelfTempTable2.txt";
            TestUtil.testScript(sessionProxy2, scriptPath);
        }
    }
}
